package simulation;

/**
 * @author cwhalen
 * 
 */
public class Node
{
	public float degLatTop;
	public float degLatBottom;
	public float degLonLeft;
	public float degLonRight;
	public float area;
	public float height;
	public float topLength;
	public float sideLength;
	public float bottomLength;
	public float oldTemp;
	public float newTemp;


	/**
	 * Default temperature for the node is 15 degrees celcius
	 */
	public Node()
	{
		this.oldTemp = 15.0f;
		this.newTemp = 15.0f;
	}


	/**
	 * @param radius
	 *            radius of the Earth
	 */
	private void cLengths(int radius)
	{
		float sepDeg = degLatTop - degLatBottom;
		this.topLength = Util.cLattitudeCircum(degLatTop, radius)
				/ (360 / Math.abs(sepDeg));
		this.bottomLength = Util.cLattitudeCircum(degLatBottom, radius)
				/ (360 / Math.abs(sepDeg));
		this.sideLength = Util.cTrapezoidSideLen(topLength, bottomLength,
				height);
	}


	/**
	 * @param radius
	 */
	private void cArea(int radius)
	{
		this.area = Util.cTrapezoidArea(topLength, bottomLength, height);
	}


	/**
	 * @param radius
	 */
	private void cHeight(int radius)
	{
		this.height = Math.abs(Util.distToEquator(degLatTop, radius)
				- Util.distToEquator(degLatBottom, radius));
	}


	/**
	 * @param radius
	 */
	public void cGeometry(int radius)
	{
		this.cHeight(radius);
		this.cLengths(radius);
		this.cArea(radius);
	}


	/**
	 * @param leftTemp
	 * @param rightTemp
	 * @param topTemp
	 * @param bottomTemp
	 * @param sun
	 */
	public void cTemp(float leftTemp, float rightTemp, float topTemp,
			float bottomTemp, Sun sun)
	{

		double radFact = sun.cRadiationFactor(degLatTop, degLatBottom,
				degLonLeft, degLonRight);

		double sunTemp = 2 * radFact;
		float neighborHeat = this.cNeighborHeat(leftTemp, rightTemp, topTemp, bottomTemp);
		
		// FIXME : Need to factor in node area and side lengths
		this.newTemp = (float)(sunTemp + neighborHeat);

	}


	/**
	 * @param leftTemp
	 * @param rightTemp
	 * @param topTemp
	 * @param bottomTemp
	 * @return
	 */
	private float cNeighborHeat(float leftTemp, float rightTemp,
			float topTemp, float bottomTemp)
	{
		float totalLen = topLength + bottomLength + sideLength + sideLength;
		return ((sideLength / totalLen) * leftTemp)
				+ ((sideLength / totalLen) * rightTemp)
				+ ((topLength / totalLen) * topTemp)
				+ ((bottomLength / totalLen) * bottomTemp);
	}


	/**
	 * 
	 */
	public void swapTemp()
	{
		this.oldTemp = this.newTemp;
	}
}
